package com.surelution.projects.crm
import java.text.SimpleDateFormat

class GasCardConsumeController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	def htmlTableParserService
	
    def index = {
        redirect(action: "list", params: params)
    }
	
	def cardConsumeImport = {
		def fromLine = 1
		def file = params.gasCardConsumeFile
		int lineNum = 0
		
		BufferedReader br = new BufferedReader(new InputStreamReader(file.inputStream, "utf8"))
		def rows = htmlTableParserService.parse(br)
		rows.each {row->
			if(lineNum > fromLine) {
				println row
				float tradeSum = Float.parseFloat(row[2])
				float discount = Float.parseFloat(row[3])
				float originalSum = Float.parseFloat(row[4])
				if(row.size() > 14) {
					GasCardConsume gasCardConsume = new GasCardConsume("stationName":row[0],"tradeType":row[1],"tradeSum":tradeSum,
						"discount":discount,"originalSum":originalSum,"productionType":row[5],"oilNumber":row[6],"cardNo":row[7],
						"marketName":row[8],"openAgency":row[9],"carLicense":row[11],"clearBusinessDay":row[12],
						"actualBusinessDay":row[13],"isTrade":row[14],)
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
						gasCardConsume.tradeTime = sdf.parse(row[10])
					gasCardConsume.save()
					println gasCardConsume 
				}
			}
			lineNum++
		}
		redirect(action:"list")
	}
	
    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [gasCardConsumeInstanceList: GasCardConsume.list(params), gasCardConsumeInstanceTotal: GasCardConsume.count()]
    }

    def create = {
        def gasCardConsumeInstance = new GasCardConsume()
        gasCardConsumeInstance.properties = params
        return [gasCardConsumeInstance: gasCardConsumeInstance]
    }

    def save = {
        def gasCardConsumeInstance = new GasCardConsume(params)
        if (gasCardConsumeInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'gasCardConsume.label', default: 'GasCardConsume'), gasCardConsumeInstance.id])}"
            redirect(action: "show", id: gasCardConsumeInstance.id)
        }
        else {
            render(view: "create", model: [gasCardConsumeInstance: gasCardConsumeInstance])
        }
    }

    def show = {
        def gasCardConsumeInstance = GasCardConsume.get(params.id)
        if (!gasCardConsumeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'gasCardConsume.label', default: 'GasCardConsume'), params.id])}"
            redirect(action: "list")
        }
        else {
            [gasCardConsumeInstance: gasCardConsumeInstance]
        }
    }

    def edit = {
        def gasCardConsumeInstance = GasCardConsume.get(params.id)
        if (!gasCardConsumeInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'gasCardConsume.label', default: 'GasCardConsume'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [gasCardConsumeInstance: gasCardConsumeInstance]
        }
    }

    def update = {
        def gasCardConsumeInstance = GasCardConsume.get(params.id)
        if (gasCardConsumeInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (gasCardConsumeInstance.version > version) {
                    
                    gasCardConsumeInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'gasCardConsume.label', default: 'GasCardConsume')] as Object[], "Another user has updated this GasCardConsume while you were editing")
                    render(view: "edit", model: [gasCardConsumeInstance: gasCardConsumeInstance])
                    return
                }
            }
            gasCardConsumeInstance.properties = params
            if (!gasCardConsumeInstance.hasErrors() && gasCardConsumeInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'gasCardConsume.label', default: 'GasCardConsume'), gasCardConsumeInstance.id])}"
                redirect(action: "show", id: gasCardConsumeInstance.id)
            }
            else {
                render(view: "edit", model: [gasCardConsumeInstance: gasCardConsumeInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'gasCardConsume.label', default: 'GasCardConsume'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def gasCardConsumeInstance = GasCardConsume.get(params.id)
        if (gasCardConsumeInstance) {
            try {
                gasCardConsumeInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'gasCardConsume.label', default: 'GasCardConsume'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'gasCardConsume.label', default: 'GasCardConsume'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'gasCardConsume.label', default: 'GasCardConsume'), params.id])}"
            redirect(action: "list")
        }
    }
}
